//
//  ViewController.m
//  NSTextViewNSTextField回车shift回车区分(oc)
//
//  Created by quanhao huang on 2019/12/20.
//  Copyright © 2019 quanhao huang. All rights reserved.
//

#import "ViewController.h"

@interface ViewController()<NSTextFieldDelegate, NSTextViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
}


- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}

#pragma mark - NSTextFieldDelegate
- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
{
    if ([NSStringFromSelector(commandSelector) isEqualToString:@"insertNewline:"]) {
        
        if (([NSApplication sharedApplication].currentEvent.modifierFlags & NSEventModifierFlagShift) != 0) {
            NSLog(@"Shift-Enter detected.");
            [textView insertNewlineIgnoringFieldEditor:self];
            return YES;
        }else {
            NSLog(@"Enter detected.");
        }
    }
    return NO;
}

#pragma mark - NSTextViewDelegate
- (BOOL)textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
{
    //insertTab:   -键入tab
    //insertNewline:   -键入回车
    //deleteBackward
    
    if ([NSStringFromSelector(commandSelector) isEqualToString:@"insertNewline:"]) {
        
        if (([NSApplication sharedApplication].currentEvent.modifierFlags & NSEventModifierFlagShift) != 0) {
            NSLog(@"Shift-Enter detected.");
            [textView insertNewlineIgnoringFieldEditor:self];
            return YES;
        }else {
            NSLog(@"Enter detected.");
            return YES;
        }
    }
    return NO;
}

@end
